package com.wtwd.campus.common;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zdl
 * @Description
 * @create 2021-06-08 17:04
 */
public class ResultUtils {
    /**
     * 成功时返回
     *
     * @param data 返回的data对象
     * @return {@link RespEntity}
     */
    public static RespEntity success(Object data) {
        RespEntity respEntity = new RespEntity();
        respEntity.setCode(RespCode.SUCCESS.getCode());
        respEntity.setMsg(RespCode.SUCCESS.getMsg());
        respEntity.setData(data == null ? "" : data);
        return respEntity;
    }

    public static RespEntity success(IPage pageData) {
        RespEntity respEntity = new RespEntity();
        respEntity.setCode(RespCode.SUCCESS.getCode());
        respEntity.setMsg(RespCode.SUCCESS.getMsg());
        if (pageData == null) {
            respEntity.setData("");
            return respEntity;
        }
        Map<String, Object> data = new HashMap<>(2);
        data.put("total", pageData.getTotal());
        data.put("rows", pageData.getRecords());
        respEntity.setData(data);
        return respEntity;
    }


    /**
     * 成功时返回
     *
     * @return {@link RespEntity}
     */
    public static RespEntity success() {
        return success(null);
    }

    /**
     * 错误时返回
     *
     * @param code    错误码
     * @param message 错误信息
     * @return {@link RespEntity}
     */
    public static RespEntity error(Integer code, String message) {
        RespEntity respEntity = new RespEntity();
        respEntity.setCode(code);
        respEntity.setMsg(message);
        respEntity.setData("");
        return respEntity;
    }

    /**
     * 错误时返回
     *
     * @param RespCode 错误枚举类
     * @return {@link RespEntity}
     */
    public static RespEntity error(RespCode RespCode) {
        return error(RespCode.getCode(), RespCode.getMsg());
    }

    /**
     * 错误时返回
     *
     * @param RespCode 错误枚举类
     * @param message  错误的信息
     * @return {@link RespEntity}
     */
    public static RespEntity error(RespCode RespCode, String message) {
        return error(RespCode.getCode(), message);
    }

    /**
     * 默认的错误
     *
     * @return {@link RespEntity}
     */
    public static RespEntity error() {
        return error(RespCode.FAILED);
    }

}
